<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>uniapp页面间传参的几种方法 | Blog</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/blog/favicon.ico">
    <meta name="description" content="MyBlog">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/blog/assets/css/0.styles.61da7a09.css" as="style"><link rel="preload" href="/blog/assets/js/app.45ac8da1.js" as="script"><link rel="preload" href="/blog/assets/js/3.08e74d92.js" as="script"><link rel="preload" href="/blog/assets/js/1.f96517fa.js" as="script"><link rel="preload" href="/blog/assets/js/18.8464bb21.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.9438aad1.js"><link rel="prefetch" href="/blog/assets/js/11.5ae1d578.js"><link rel="prefetch" href="/blog/assets/js/12.5f7d5b87.js"><link rel="prefetch" href="/blog/assets/js/13.c7b5cd64.js"><link rel="prefetch" href="/blog/assets/js/14.fffadab2.js"><link rel="prefetch" href="/blog/assets/js/15.e3572620.js"><link rel="prefetch" href="/blog/assets/js/16.6fad2367.js"><link rel="prefetch" href="/blog/assets/js/17.20d5fc40.js"><link rel="prefetch" href="/blog/assets/js/19.57eca0d1.js"><link rel="prefetch" href="/blog/assets/js/20.2f642e3b.js"><link rel="prefetch" href="/blog/assets/js/21.4a56260b.js"><link rel="prefetch" href="/blog/assets/js/4.6ccf2801.js"><link rel="prefetch" href="/blog/assets/js/5.d9c29fca.js"><link rel="prefetch" href="/blog/assets/js/6.510ee365.js"><link rel="prefetch" href="/blog/assets/js/7.79ac4d06.js"><link rel="prefetch" href="/blog/assets/js/8.ddfc50a4.js"><link rel="prefetch" href="/blog/assets/js/9.0724da36.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.61da7a09.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-7dd95ae2><div data-v-7dd95ae2><div class="password-shadow password-wrapper-out" style="display:none;" data-v-59e6cb88 data-v-7dd95ae2 data-v-7dd95ae2><h3 class="title" data-v-59e6cb88>Blog</h3> <p class="description" data-v-59e6cb88>MyBlog</p> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>Brod-Roy</span>
          
        <!---->
        2022
      </a></span></div></div> <div class="hide" data-v-7dd95ae2><header class="navbar" data-v-7dd95ae2><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/logo.png" alt="Blog" class="logo"> <span class="site-name">Blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/frontEnd/" class="nav-link"><i class="undefined"></i>
  frontEnd
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/前端学习/" class="nav-link"><i class="undefined"></i>
  前端学习
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/踩坑填坑/" class="nav-link"><i class="undefined"></i>
  踩坑填坑
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/工作分享/" class="nav-link"><i class="undefined"></i>
  工作分享
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间索引
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      相关内容
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/Brod_Roy" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-7dd95ae2></div> <aside class="sidebar" data-v-7dd95ae2><div class="personal-info-wrapper" data-v-1fad0c41 data-v-7dd95ae2><img src="/blog/avatar.jpg" alt="author-avatar" class="personal-img" data-v-1fad0c41> <h3 class="name" data-v-1fad0c41>
    Brod-Roy
  </h3> <div class="num" data-v-1fad0c41><div data-v-1fad0c41><h3 data-v-1fad0c41>11</h3> <h6 data-v-1fad0c41>Articles</h6></div> <div data-v-1fad0c41><h3 data-v-1fad0c41>8</h3> <h6 data-v-1fad0c41>Tags</h6></div></div> <ul class="social-links" data-v-1fad0c41></ul> <hr data-v-1fad0c41></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/frontEnd/" class="nav-link"><i class="undefined"></i>
  frontEnd
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/前端学习/" class="nav-link"><i class="undefined"></i>
  前端学习
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/踩坑填坑/" class="nav-link"><i class="undefined"></i>
  踩坑填坑
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/工作分享/" class="nav-link"><i class="undefined"></i>
  工作分享
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间索引
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      相关内容
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://gitee.com/Brod_Roy" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-59e6cb88 data-v-7dd95ae2><h3 class="title" data-v-59e6cb88>uniapp页面间传参的几种方法</h3> <!----> <label id="box" class="inputBox" data-v-59e6cb88><input type="password" value="" data-v-59e6cb88> <span data-v-59e6cb88>Konck! Knock!</span> <button data-v-59e6cb88>OK</button></label> <div class="footer" data-v-59e6cb88><span data-v-59e6cb88><i class="iconfont reco-theme" data-v-59e6cb88></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-59e6cb88>vuePress-theme-reco</a></span> <span data-v-59e6cb88><i class="iconfont reco-copyright" data-v-59e6cb88></i> <a data-v-59e6cb88><span data-v-59e6cb88>Brod-Roy</span>
          
        <!---->
        2022
      </a></span></div></div> <div data-v-7dd95ae2><div data-v-7dd95ae2><main class="page" style="padding-right:0;"><section style="display:;"><div class="page-title"><h1 class="title">uniapp页面间传参的几种方法</h1> <div data-v-8a445198><i class="iconfont reco-account" data-v-8a445198><span data-v-8a445198>Brod-Roy</span></i> <i class="iconfont reco-date" data-v-8a445198><span data-v-8a445198>11/5/2022</span></i> <!----> <i class="tags iconfont reco-tag" data-v-8a445198><span class="tag-item" data-v-8a445198>uniapp</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="uniapp页面间传参的几种方法"><a href="#uniapp页面间传参的几种方法" class="header-anchor">#</a> uniapp页面间传参的几种方法</h1> <blockquote><p>在Uniapp中的传参主要分为以下三种类型：</p> <ol><li>上级页面  →  下级页面（单向）</li> <li>上级页面  ←  下级页面（单向）</li> <li>上级页面  ↔  下级页面（双向）</li></ol> <p>本文将围绕这三种传参展开，分享其使用方法以及我使用中所遇到的问题与解决方法。</p></blockquote> <h2 id="一、上级页面-→-下级页面-单向"><a href="#一、上级页面-→-下级页面-单向" class="header-anchor">#</a> 一、上级页面  →  下级页面（单向）</h2> <h3 id="uni-navigateto-url编程式传参"><a href="#uni-navigateto-url编程式传参" class="header-anchor">#</a> uni.navigateTo:URL编程式传参</h3> <blockquote><p>作为最常用也是最简单的跳转携带参数的API，这里不多赘述，想详细了解的朋友可以前往官方文档学习，这里只做传参分享。</p> <p>官方文档：<a href="https://uniapp.dcloud.net.cn/api/router.html#navigateto" target="_blank" rel="noopener noreferrer">uni.navigateTo(OBJECT)<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote> <h4 id="携带静态参数"><a href="#携带静态参数" class="header-anchor">#</a> 携带静态参数</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//在起始页面跳转到test.vue页面并传递参数</span>
<span class="token comment">//作用场景，需要提供固定传参状态的页面，一般和动态参数一起使用</span>
uni<span class="token punctuation">.</span><span class="token function">navigateTo</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
	<span class="token literal-property property">url</span><span class="token operator">:</span> <span class="token string">'test?id=1&amp;name=uniapp'</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="携带动态参数"><a href="#携带动态参数" class="header-anchor">#</a> 携带动态参数</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//在起始页面跳转到test.vue页面并传递参数</span>
<span class="token keyword">let</span> uniapp <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">uniappItem</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">//当传递的参数是对象时，必须先转化为JSON格式</span>
uni<span class="token punctuation">.</span><span class="token function">navigateTo</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
	<span class="token literal-property property">url</span><span class="token operator">:</span> <span class="token string">'test?id=1&amp;name='</span> <span class="token operator">+</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>uniapp<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h4 id="页面接收"><a href="#页面接收" class="header-anchor">#</a> 页面接收</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 在test.vue页面接受参数</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
	<span class="token function-variable function">onLoad</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">option</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//option为object类型，会序列化上个页面传递的参数</span>
		console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>option<span class="token punctuation">.</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//打印出上个页面传递的参数。</span>
		console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>option<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//打印出上个页面传递的参数。</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="navigator-标签传参"><a href="#navigator-标签传参" class="header-anchor">#</a> &lt;navigator&gt;标签传参</h3> <blockquote><p>URL有长度限制，太长的字符串会传递失败，可改用<a href="https://uniapp.dcloud.io/collocation/frame/communication" target="_blank" rel="noopener noreferrer">窗体通信<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>、<a href="https://ask.dcloud.net.cn/article/35021" target="_blank" rel="noopener noreferrer">全局变量<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，另外参数中出现空格等特殊字符时需要对参数进行编码，如下为使用<code>encodeURIComponent</code>对参数进行编码的示例。</p></blockquote> <h4 id="标签传参"><a href="#标签传参" class="header-anchor">#</a> 标签传参</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//此处的 :URL 是动态载入，参数是变量;</span>
<span class="token comment">//当使用了 :URl 却使用了静态地址，有可能不生效，同样如果使用了变量却没有用 :URL 也会有问题</span>
<span class="token operator">&lt;</span>navigator <span class="token operator">:</span>url<span class="token operator">=</span><span class="token string">&quot;'/pages/test/test?item='+ encodeURIComponent(JSON.stringify(item))&quot;</span><span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>navigator<span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="页面接收-2"><a href="#页面接收-2" class="header-anchor">#</a> 页面接收</h4> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 在test.vue页面接受参数</span>
<span class="token function-variable function">onLoad</span><span class="token operator">:</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">option</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">const</span> item <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token function">decodeURIComponent</span><span class="token punctuation">(</span>option<span class="token punctuation">.</span>item<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="二、上级页面-←-下级页面-单向"><a href="#二、上级页面-←-下级页面-单向" class="header-anchor">#</a> 二、上级页面  ←  下级页面（单向）</h2> <blockquote><p>一般来说 uni.navigateTo 的参数传递可以满足页面的传递，但遇到需要更新上级页面的需求时，就需要使用uni.$emit()和uni.$on() 进行页面间通讯。</p> <p>该方法一般运用在当你从下级页面（或组件）改变数据后，通知上级页面进行刷新或其他操作，实在不清楚也没关系，当你需要时自然明白。</p></blockquote> <h3 id="uni-on-eventname-callback-监听事件"><a href="#uni-on-eventname-callback-监听事件" class="header-anchor">#</a> uni.$on(eventName,callback)：监听事件</h3> <blockquote><p>在上级页面设置 <code>uni.$emit()</code> 来监听下级页面的调用，其中的eventName就是事件名称，第二个参数是接受到函数后触发的回调函数。在监听事件结束后一定要移除监听事件，不然会有重复监听的问题。</p></blockquote> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 我的页面  </span>
<span class="token function">onLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>  
    <span class="token comment">// 监听事件  </span>
    uni<span class="token punctuation">.</span><span class="token function">$on</span><span class="token punctuation">(</span><span class="token string">'login'</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token parameter">usnerinfo</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>  
        <span class="token keyword">this</span><span class="token punctuation">.</span>usnerinfo <span class="token operator">=</span> usnerinfo<span class="token punctuation">;</span>  
    <span class="token punctuation">}</span><span class="token punctuation">)</span>  
<span class="token punctuation">}</span><span class="token punctuation">,</span>  
<span class="token function">onUnload</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>  
    <span class="token comment">// 移除监听事件  </span>
    uni<span class="token punctuation">.</span><span class="token function">$off</span><span class="token punctuation">(</span><span class="token string">'login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="触发事件"><a href="#触发事件" class="header-anchor">#</a> 触发事件</h3> <blockquote><p>传递的参数一定是要在对象中的属性</p></blockquote> <div class="language-js line-numbers-mode"><pre class="language-js"><code>uni<span class="token punctuation">.</span><span class="token function">$emit</span><span class="token punctuation">(</span><span class="token string">'login'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>  
	<span class="token literal-property property">avatarUrl</span><span class="token operator">:</span> <span class="token string">'https://img-cdn-qiniu.dcloud.net.cn/uploads/nav_menu/10.jpg'</span><span class="token punctuation">,</span>  
	<span class="token literal-property property">token</span><span class="token operator">:</span> <span class="token string">'user123456'</span><span class="token punctuation">,</span>  
	<span class="token literal-property property">userName</span><span class="token operator">:</span> <span class="token string">'unier'</span><span class="token punctuation">,</span>  
	<span class="token literal-property property">login</span><span class="token operator">:</span> <span class="token boolean">true</span>  
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="三、上级页面-↔-下级页面-双向"><a href="#三、上级页面-↔-下级页面-双向" class="header-anchor">#</a> 三、上级页面  ↔  下级页面（双向）</h2> <blockquote><p>在一般情况下，单向传递已经可以满足我们的业务需求，如向下级页面传递需要显示的参数、变动的状态，向上级页面传递的需要更新的数据或是需要再次调用的函数。</p> <p>但遇到两个联系十分紧密的页面时，单向传递就无法满足我们的业务需求，向下级页面传递参数和监听事件就会显得特别繁琐。在uniapp中，它将上面两种传递方式加以结合，提供了这样一个方法去实现双向传递：<code>uni.navigateTo({ event:{} })</code></p></blockquote> <h3 id="上级页面内代码"><a href="#上级页面内代码" class="header-anchor">#</a> 上级页面内代码</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 在起始页面跳转到test.vue页面，并监听test.vue发送过来的事件数据</span>
uni<span class="token punctuation">.</span><span class="token function">navigateTo</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token literal-property property">url</span><span class="token operator">:</span> <span class="token string">'pages/test?id=1'</span><span class="token punctuation">,</span>
  <span class="token comment">// 调用通信事件对象</span>
  <span class="token literal-property property">events</span><span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token comment">// 获取下级页面参数：</span>
    <span class="token comment">// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据</span>
    <span class="token comment">// 注意看下级页面中所对应的函数名，你可以定义多个方法去管理传递的参数</span>
    <span class="token function-variable function">acceptDataFromOpenedPage</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>、
      <span class="token comment">// 对数据做处理</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token function-variable function">someEvent</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// 对数据做处理</span>
      console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token comment">// 发送通信方法</span>
  <span class="token function-variable function">success</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">res</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 通过eventChannel向被打开页面传送数据</span>
    <span class="token comment">// 其中含有两个参数，第一个是接收的函数名，第二个则是需要携带的参数</span>
    res<span class="token punctuation">.</span>eventChannel<span class="token punctuation">.</span><span class="token function">emit</span><span class="token punctuation">(</span><span class="token string">'acceptDataFromOpenerPage'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token string">'data from starter page'</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h3 id="下级页面内代码"><a href="#下级页面内代码" class="header-anchor">#</a> 下级页面内代码</h3> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 在test.vue页面，向起始页通过事件传递数据</span>
<span class="token comment">// 此方法不是一定要在 onLoad 内调用，哪里需要哪里调</span>
<span class="token function-variable function">onLoad</span><span class="token operator">:</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">option</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// 此处声明只是为了显示看起来简洁一点</span>
  <span class="token keyword">const</span> eventChannel <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">getOpenerEventChannel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// emit 代表的就是向上一个页面传递需要更新的数据</span>
  eventChannel<span class="token punctuation">.</span><span class="token function">emit</span><span class="token punctuation">(</span><span class="token string">'acceptDataFromOpenedPage'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token string">'data from test page'</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  eventChannel<span class="token punctuation">.</span><span class="token function">emit</span><span class="token punctuation">(</span><span class="token string">'someEvent'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token literal-property property">data</span><span class="token operator">:</span> <span class="token string">'data from test page for someEvent'</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token comment">// 接收上个页面传递的数据</span>
  <span class="token comment">// 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据</span>
  eventChannel<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'acceptDataFromOpenerPage'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 对数据做处理</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div></div></section> <footer class="page-edit"><!----> <!----></footer> <!----> <div class="comments-wrapper"><!----></div></main></div> <!----></div> <ul class="sub-sidebar sub-sidebar-wrapper" style="width:0;" data-v-b57cc07c data-v-7dd95ae2></ul></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/blog/assets/js/app.45ac8da1.js" defer></script><script src="/blog/assets/js/3.08e74d92.js" defer></script><script src="/blog/assets/js/1.f96517fa.js" defer></script><script src="/blog/assets/js/18.8464bb21.js" defer></script>
  </body>
</html>
